﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;


public class DataCfgBase
{
    //public string AssetPath = Application.dataPath + "/Resources/Settings/";

    public string DirectoryPath = "ConfigData/";

    public Dictionary<int, RowDataBase> datas = new Dictionary<int, RowDataBase>();

    public virtual RowDataBase LoadDataInfo(GameDataTableParser data) { return null; }
    public virtual string GetFilePath() { return string.Empty; }

    public virtual string GetAssetPath() { return string.Empty; }
    public virtual void GenerateAsset() { }

    public void Load()
    {
        datas.Clear();
        TextAsset data = Resources.Load(GetFilePath()) as TextAsset;
        using (GameDataTableParser gameDataTable = new GameDataTableParser()) //注意：一定要using()，这样才会自动调用这个类的Dispose()方法！！！！！！！
        {
            gameDataTable.ReadTableFromTextAsset(data, true);

            gameDataTable.first();
            while (!gameDataTable.eof())
            {
                RowDataBase info = LoadDataInfo(gameDataTable);
                datas.Add(info.id, info);
                gameDataTable.next();
            }
        }

    }


    public void Load(byte[] byteData)
    {
        datas.Clear();

        using (GameDataTableParser gameDataTable = new GameDataTableParser()) //注意：一定要using()，这样才会自动调用这个类的Dispose()方法！！！！！！！
        {
            gameDataTable.ReadTableFromBytes(byteData, true);

            gameDataTable.first();
            while (!gameDataTable.eof())
            {
                RowDataBase info = LoadDataInfo(gameDataTable);
                datas.Add(info.id, info);
                gameDataTable.next();
            }
            HandData();
        }
    }

    public RowDataBase GetOneData(int id)
    {
        RowDataBase tem = null;
        foreach (int key in datas.Keys)
        {
            if (datas[key].id == id)
            {
                return datas[key];
            }
        }
        Debug.LogError("获取EnemyCfg为空，id为：" + id + "!");
        return tem;
    }

    public RowDataBase GetData(int id)
    {
        if (datas.ContainsKey(id))
        {
            return datas[id];
        }
        return null;
    }

    /// <summary>
    /// 读取表中数据
    /// </summary>
    public virtual IEnumerator LoadDataByPath(string Path)
    {
        //1.选择加载本地的配置文件
        return HotUpdateTools.ClientWWWLoad(Path, callbackLoadClientByte);

        //2.选择加载网络的配置文件
        //return HotUpdateTools.ServerWWWLoad(Path, callbackLoadClientByte);
    }
    public virtual IEnumerator LoadDataByWWW(string Path)
    {
        return HotUpdateTools.ServerWWWLoad(Path, callbackLoadClientByte);
    }

    /// <summary>
    /// 处理读表后的数据
    /// </summary>
    /// <param name="bundle"></param>
    protected virtual void callbackLoadClientByte(WWW bundle)
    {
        if (bundle != null && bundle.size != 0)
        {
            Load(bundle.bytes);
            bundle.Dispose();
        }
    }

    /// <summary>
    /// 读取数据后进行预处理，主要是重新排序
    /// </summary>
    protected virtual void HandData()
    {

    }

    protected virtual void resetData()
    {
        datas.Clear();
    }
}
